/**
 *	@file	safario_sub.c
 *	@brief	サファリOBJレイアウトツール　サブ
 *	@author	Miyuki Iwasawa
 *	@date	08.03.13
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/window.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/wordset.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "gflib/msg_print.h"
#include "gflib/strbuf_family.h"
#include "application/app_tool.h"
#include "savedata/config.h"

#include "application/safari_l.h"

#include "safario_sys.h"
#include "safario_sub.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_safari_o.h"
#include "safario_gra.naix"

#include "data/clact_util/clact_header.naix"

//================================================================
//----------------------------------------------------------------
///定義
//----------------------------------------------------------------
//================================================================
typedef struct _PAGE_CHANGE_TASK{
	int* tcb_ct;	//タスクの終了を検知するためのカウンタ。TRUEなら再生中。NULL可

	SAFARIO_WORK* app_wk;
	u8 dir;
	u8 ct;
	u16 seq;
	CLACT_WORK_PTR clact_obj;
	CATS_ACT_PTR cap_obj;
}PAGE_CHANGE_TASK;


static const RECT_HIT_TBL Panel_TpRect[] = {
//		{0,191,0,255}, ty,by,lx,rx
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL_PX01,TP_PANEL_PX01+TP_PANEL_SX},
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL_PX02,TP_PANEL_PX02+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL_PX01,TP_PANEL_PX01+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL_PX02,TP_PANEL_PX02+TP_PANEL_SX},
 {TP_PANEL_PY03,TP_PANEL_PY03+TP_PANEL_SY,TP_PANEL_PX01,TP_PANEL_PX01+TP_PANEL_SX},
 {TP_PANEL_PY03,TP_PANEL_PY03+TP_PANEL_SY,TP_PANEL_PX02,TP_PANEL_PX02+TP_PANEL_SX},
 {TP_BACK_PY,TP_BACK_PY+TP_BACK_SY,TP_BACK_PX,TP_BACK_PX+TP_BACK_SX},
 {TP_ALLOW_PY,TP_ALLOW_PY+TP_ALLOW_SY,TP_ALLOWL_PX,TP_ALLOWL_PX+TP_ALLOW_SX},
 {TP_ALLOW_PY,TP_ALLOW_PY+TP_ALLOW_SY,TP_ALLOWR_PX,TP_ALLOWR_PX+TP_ALLOW_SX},
 {RECT_HIT_END,0,0,0}
};

static const TCATS_OAM_INIT safario_coi = {
	0,128,		///< メイン	OAM管理領域・開始/終了
	0,32,		///< メイン	アフィン管理領域・開始/終了
	0,128,		///< サブ	OAM管理領域・開始/終了
	0,32,		///< サブ	アフィン管理領域・開始/終了
};
static const TCATS_CHAR_MANAGER_MAKE safario_ccmm = {
	SAFARIO_ACTRES_MAX,	//ID制御数
	16*1024,	//メイン画面サイズ(byte単位) 64kbyte
	16*1024,	//サブ画面サイズ(byte単位) 16kbyte
	GX_OBJVRAMMODE_CHAR_1D_32K,	//メインOBJモード指定
	GX_OBJVRAMMODE_CHAR_1D_32K,	//サブOBJモード指定
};
//TCATS_RES_ARC
static const TCATS_RESOURCE_FILE_LIST safario_actres_list = {
	NARC_clact_header_safario_chr_resdat,		// キャラクタ
	NARC_clact_header_safario_pal_resdat,		// パレット
	NARC_clact_header_safario_cell_resdat,		// セル
	NARC_clact_header_safario_canm_resdat,		// セルアニメ
	TCATS_RESID_NULL,							// マルチセル
	TCATS_RESID_NULL,							// マルチセルアニメ
	NARC_clact_header_safario_celact_cldat		// ヘッダー
};

//================================================================
//----------------------------------------------------------------
///プロトタイプ
//----------------------------------------------------------------
//================================================================
static int exe_ObjSelect(SAFARIO_WORK* wk,u8 code);
static int keyin_ObjSelect(SAFARIO_WORK* wk);
static int touchin_ObjSelect(SAFARIO_WORK* wk,BOOL* exe_f);

static void sub_VBlank(void* work);
static void sub_BGLInit(SAFARIO_WORK* wk);
static void sub_BGLRelease(SAFARIO_WORK* wk);
static void sub_2DResInit(SAFARIO_WORK* wk);
static void sub_2DResRelease(SAFARIO_WORK* wk);
static void sub_3DResInit(SAFARIO_WORK* wk);
static void sub_3DResRelease(SAFARIO_WORK* wk);
static void sub_MsgDataInit(SAFARIO_WORK* wk);
static void sub_MsgDataRelease(SAFARIO_WORK* wk);
static void sub_BmpWinAdd(SAFARIO_WORK* wk);
static void sub_BmpWinDelete(SAFARIO_WORK* wk);
static void sub_ActResInit(SAFARIO_WORK* wk);
static void sub_ActResRelease(SAFARIO_WORK* wk);
static void sub_MakeActor(SAFARIO_WORK* wk);
static void sub_DeleteActor(SAFARIO_WORK* wk);

static void sub_ExpWinFrameDraw(SAFARIO_WORK* wk,u8 pat);
static void sub_ExpWinDraw(SAFARIO_WORK* wk,u8 widx,u8 midx,u8 draw_f);
static void sub_PageViewSet(SAFARIO_WORK* wk);
static void sub_BackViewSet(SAFARIO_WORK* wk,u8 mode);
static void sub_TargetViewSet(SAFARIO_WORK* wk);

static void sub_BackButtonWrite(SAFARIO_WORK* wk,BACK_BTN_STATE state);
static void sub_CursorVisibleSet(SAFARIO_WORK* wk,u8 idx,u8 pat,BOOL draw_f);
static void sub_CursorPosSet(SAFARIO_WORK* wk,u8 pos);
static void sub_SysMsgPut(SAFARIO_WORK* wk,SYSMSG_IDX idx);
static void sub_TalkMsgPut(SAFARIO_WORK* wk,TALKMSG_IDX idx);
static void sub_ExpMsgPut(SAFARIO_WORK* wk);
static void sub_InitialDraw(SAFARIO_WORK* wk);

static void eff_PageScrollInit(SAFARIO_WORK* wk);
static BOOL eff_PageScroll(SAFARIO_WORK* wk);

static int sub_CheckNotPlaceObj(SAFARIO_WORK* wk,u8 id);
TCB_PTR sub_PageChangeTaskSet(SAFARIO_WORK* wk,u8 dir,int* tcb_ct,u8 pri);
static void tcb_PageChangeTask( TCB_PTR tcb, void* p_work );
static void sub_ModelChangeReq(SAFARIO_WORK* wk);

//================================================================
//----------------------------------------------------------------
///プログラムエリア
//----------------------------------------------------------------
//================================================================

//----------------------------------------------------------------
///グローバルエリア
//----------------------------------------------------------------
/**
 *	@brief	アプリケーション初期化
 *
 *	@retval	TRUE	初期化終了
 *	@retval FALSE	初期化中
 */
BOOL SafariObjSeq_AppInit(SAFARIO_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		sub_BGLInit(wk);
		sub_2DResInit(wk);
		sub_3DResInit(wk);
		sub_MsgDataInit(wk);
		sub_BmpWinAdd(wk);
		sub_ActResInit(wk);
		sub_MakeActor(wk);
		break;
	case 1:
		sub_InitialDraw(wk);
		sys_VBlankFuncChange(sub_VBlank,wk);

		//タッチパネル初期化
		InitTPSystem();
		InitTPNoBuff(4);

		wk->sub_seq = 0;
		return TRUE;
	}
	wk->sub_seq++;
	return FALSE;
}

/**
 *	@brief	アプリケーション解放
 *
 *	@retval	TRUE	解放終了
 *	@retval FALSE	解放中
 */
BOOL SafariObjSeq_AppEnd(SAFARIO_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		sys_VBlankFuncChange(NULL, NULL);
		sys_HBlankIntrStop();	//HBlank割り込み停止

		sub_DeleteActor(wk);
		sub_ActResRelease(wk);
		sub_BmpWinDelete(wk);
		sub_MsgDataRelease(wk);
		sub_3DResRelease(wk);
		sub_2DResRelease(wk);
		sub_BGLRelease(wk);
		break;
	case 2:
		//タッチパネル終了
		StopTP();

		GX_SetVisiblePlane(0);
		GXS_SetVisiblePlane(0);
		GF_Disp_GX_VisibleControlInit();
		GF_Disp_GXS_VisibleControlInit();

		wk->sub_seq = 0;
		return TRUE;
	}
	wk->sub_seq++;
	return FALSE;
}

/**
 *	@brief	オブジェ選択画面　キー取得
 */
int SafariObj_Input(SAFARIO_WORK* wk)
{
	int ret,exe_f = FALSE;

	ret = touchin_ObjSelect(wk,&exe_f);

	if(!exe_f){
		ret = keyin_ObjSelect(wk);
	}
	return ret;
}

/**
 *	@brief	オブジェ選択画面　メッセージ表示待ち
 */
int SafariObj_MsgWait(SAFARIO_WORK* wk)
{
	BOOL sw_f = FALSE;
	if( GF_TP_GetTrg()){
		wk->key_mode = APP_KTST_TOUCH;
		sw_f = TRUE;
	}else{
		if(sys.trg & PAD_BUTTON_KTST_CHG){
			wk->key_mode = APP_KTST_KEY;
		}
		if(sys.trg & (PAD_BUTTON_DECIDE|PAD_BUTTON_CANCEL)){
			sw_f = TRUE;
		}
	}
	if(sw_f){
		Snd_SePlay( SE_SAFARIO_SELECT );
		BmpTalkWinClear( &wk->win[WIN_TMSG],WINDOW_TRANS_OFF);
//		GF_BGL_BmpWinDataFill(&wk->win[WIN_TMSG], FBMP_COL_NULL);
		GF_BGL_BmpWinOffVReq(&wk->win[WIN_TMSG]);
		return LSEQ_NULL;
	}
	return LSEQ_MSG_WAIT;
}

/**
 *	@brief	ターゲットViewセット
 */
void SafariObj_ViewChange(SAFARIO_WORK* wk,u8 mode)
{
	BOOL on_f;
	if(mode == VIEW_TARGET){
		sub_BackViewSet(wk,VIEW_TARGET);
		sub_BackButtonWrite(wk,BACK_BTN_PASSIVE);
		sub_TargetViewSet(wk);
		sub_SysMsgPut(wk,SYSMSG_IS_PLACE);
		on_f = FALSE;
	}else{
		sub_BackViewSet(wk,VIEW_SELECT);
		GF_BGL_BmpWinOffVReq(&wk->win[WIN_TARGET]);
		sub_BackButtonWrite(wk,BACK_BTN_ACTIVE);
		sub_PageViewSet(wk);
		sub_SysMsgPut(wk,SYSMSG_REST_NUM);
		on_f = TRUE;
	}
	CLACT_SetDrawFlag(wk->pAct[ACT_ALLOW_L], on_f );
	CLACT_SetDrawFlag(wk->pAct[ACT_ALLOW_R], on_f );
	CLACT_SetDrawFlag(wk->pAct[ACT_CURSOR], on_f );
}

/**
 *	@brief	YesNoボタン初期化
 */
void SafariObj_YesNoBtnSet(SAFARIO_WORK* wk)
{
	TOUCH_SW_PARAM param;
	MI_CpuClear8(&param,sizeof(TOUCH_SW_PARAM));

	param.p_bgl  = wk->bgl;
	param.bg_frame = BMPL_YESNO_FRM;
	param.char_offs = BMPL_YESNO_CGX;
	param.pltt_offs = BMPL_YESNO_PAL;
	param.x		= BMPL_YESNO_PX;
	param.y		= BMPL_YESNO_PY;
	param.kt_st = wk->key_mode;
	param.key_pos = 0;
	param.type = TOUCH_SW_TYPE_S;
	TOUCH_SW_InitEx( wk->ynbtn_wk, &param , NULL);
}

/**
 *	@brief	YesNoボタン待ち
 */
int SafariObj_YesNoBtnWait(SAFARIO_WORK* wk)
{
	int ret;
	int	mode;

	ret = TOUCH_SW_Main( wk->ynbtn_wk );

	switch(ret){
	case TOUCH_SW_RET_YES:
		ret = 1;
		break;
	case TOUCH_SW_RET_NO:
		ret = 0;
		break;
	default:
		return -1;
	}
	wk->key_mode = TOUCH_SW_GetKTStatus(wk->ynbtn_wk);
	TOUCH_SW_Reset( wk->ynbtn_wk);

#if 0
	BmpTalkWinClear( &wk->win[WIN_MSG], WINDOW_TRANS_OFF);
	GF_BGL_BmpWinDataFill( & wk->win[WIN_MSG], 0 );
	GF_BGL_BmpWinOnVReq( &wk->win[WIN_MSG]);
	GF_BGL_LoadScreenV_Req( wk->bgl, DFRM_MSG );
#endif
	return ret;
}

//----------------------------------------------------------------
///ローカルエリア
//----------------------------------------------------------------
static int exe_ObjSelect(SAFARIO_WORK* wk,u8 code)
{
	u8 ret;
	if(code >= CPOS_BACK){
		Snd_SePlay( SE_SAFARIO_CANCEL );
		sub_BackButtonWrite(wk,BACK_BTN_ANM);
		wk->next_seq = SEQ_FADEOUT;
		return LSEQ_END;
	}
	wk->target = wk->page*PAGE_ITEM_MAX+wk->pos;
	if(wk->target >= wk->list_max){
		return LSEQ_NULL;
	}
	ret = sub_CheckNotPlaceObj(wk,wk->target);
	if(ret){
		Snd_SePlay( SE_SAFARIO_NG );
		sub_TalkMsgPut(wk,ret-1);
		return LSEQ_MSG_WAIT;
	}
	Snd_SePlay( SE_SAFARIO_DECIDE );
	wk->target_obj = wk->obj[wk->target].id;
	return LSEQ_PLACE_CHK;
}

/*
 *	@brief	キー取得ブロック　キー
 */
static int keyin_ObjSelect(SAFARIO_WORK* wk)
{
	u8 px,py,page_mv = 0;

	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}

#ifdef PM_DEBUG
	if(Debug_SafariObj_CameraSet(&wk->sys3d)){
		return LSEQ_NULL;
	}
#endif

	if(sys.trg & PAD_BUTTON_CANCEL){
		return exe_ObjSelect(wk,CPOS_BACK);
	}
	if(sys.trg & PAD_BUTTON_DECIDE){
		return exe_ObjSelect(wk,wk->pos);
	}

	if(!(sys.trg & PAD_KEY_ALL)){
		return LSEQ_NULL;
	}
	px = wk->pos % 2;
	py = wk->pos / 2;

	if(sys.trg & PAD_KEY_RIGHT){
		if(py == 3){
			return LSEQ_NULL;
		}
		if(px == 1){
			if(wk->page < (wk->page_max-1) && py < 3){
				wk->page++;
				page_mv = 2;
			}else{
				return LSEQ_NULL;
			}
		}else{
			px++;
		}
	}else if(sys.trg & PAD_KEY_LEFT){
		if(py == 3){
			return LSEQ_NULL;
		}
		if(px == 0){
			if(wk->page > 0 && py < 3){
				wk->page--;
				page_mv = 1;
			}else{
				return LSEQ_NULL;
			}
		}else{
			px--;
		}
	}else if(sys.trg & PAD_KEY_UP){
		py = (py+3)%4;
	}else if(sys.trg & PAD_KEY_DOWN){
		py = (py+1)%4;
	}
	wk->pos = py*2+px;

	if(page_mv){
		sub_PageChangeTaskSet(wk,page_mv-1,&wk->tcb_ct,0);
		return LSEQ_NULL;
	}
	Snd_SePlay( SE_SAFARIO_SELECT );
	sub_CursorPosSet(wk,wk->pos);
	sub_ExpMsgPut(wk);

	return LSEQ_NULL;
}
static int touchin_ObjSelect(SAFARIO_WORK* wk,BOOL* exe_f)
{
	int ret;
	u16	pat;

	ret = GF_TP_RectHitTrg(Panel_TpRect);
	if(	ret == RECT_HIT_NONE){
		*exe_f = FALSE;
		return LSEQ_NULL;
	}
	if(ret == CPOS_LEFT){
		if(wk->page > 0){
			wk->page--;
			sub_PageChangeTaskSet(wk,PAGE_DOWN,&wk->tcb_ct,0);
			*exe_f = TRUE;
		}
		return LSEQ_NULL;
	}else if(ret == CPOS_RIGHT){
		if(wk->page < (wk->page_max-1)){
			wk->page++;
			sub_PageChangeTaskSet(wk,PAGE_UP,&wk->tcb_ct,0);
			*exe_f = TRUE;
		}
		return LSEQ_NULL;
	}
	//選択リスト描画
	if(ret != wk->pos){
		wk->pos = ret;
		sub_CursorPosSet(wk,wk->pos);
		sub_ExpMsgPut(wk);
	}
	*exe_f = TRUE;
	return exe_ObjSelect(wk,wk->pos);
}

/**
 *	@brief	サファリOBJレイアウト VBlank関数
 */
static void sub_VBlank(void* work)
{
	SAFARIO_WORK* wk = (SAFARIO_WORK*)work;

	if(wk->mdl_chg_req){
		u8 target;
		target = wk->page*PAGE_ITEM_MAX+wk->pos;
		if(wk->pos >= PAGE_ITEM_MAX || target >= wk->list_max){
			SafariObj_ModelChange(&wk->sys3d,SAFARI_OBJ_ID_MAX,NULL);
		}else{
			SafariObj_ModelChange(&wk->sys3d,wk->obj[target].id,&wk->obj[target].prm);
		}
		wk->mdl_chg_req = FALSE;
	}

	//アクター転送
	if(wk->pActSys != NULL){
		CATS_RenderOamTrans();
	}

	DoVramTransferManager();
	GF_BGL_VBlankFunc( wk->bgl );
	OS_SetIrqCheckFlag( OS_IE_V_BLANK);
}

/**
 *	@brief	サファリOBJレイアウトVramBank設定
 */
static void sub_VBankSet(void)
{
	GF_BGL_DISPVRAM vramSetTable = {
		GX_VRAM_BG_128_B,				// メイン2DエンジンのBG
		GX_VRAM_BGEXTPLTT_NONE,			// メイン2DエンジンのBG拡張パレット
		GX_VRAM_SUB_BG_128_C,			// サブ2DエンジンのBG
		GX_VRAM_SUB_BGEXTPLTT_NONE,		// サブ2DエンジンのBG拡張パレット
		GX_VRAM_OBJ_16_G,				// メイン2DエンジンのOBJ
		GX_VRAM_OBJEXTPLTT_NONE,		// メイン2DエンジンのOBJ拡張パレット
		GX_VRAM_SUB_OBJ_16_I,			// サブ2DエンジンのOBJ
		GX_VRAM_SUB_OBJEXTPLTT_NONE,	// サブ2DエンジンのOBJ拡張パレット
		GX_VRAM_TEX_0_A,				// テクスチャイメージスロット
		GX_VRAM_TEXPLTT_0_F				// テクスチャパレットスロット
	};
	GF_Disp_SetBank( &vramSetTable );
}


/**
 *	@brief	BGL初期化
 */
static void sub_BGLInit(SAFARIO_WORK* wk)
{
	int i,frame;
	GF_BGL_INI* bgl;

	//VramBankセット
	sub_VBankSet();

	//上画面をメイン,下画面をサブに設定
	GX_SetDispSelect(GX_DISP_SELECT_MAIN_SUB);

	//BGL初期化
	wk->bgl = GF_BGL_BglIniAlloc(wk->heapID);

	{	//BG SYSTEM
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS,GX_BGMODE_0,GX_BGMODE_0,GX_BG0_AS_3D
		};
		GF_BGL_InitBG(&BGsys_data);
	}

	{	// 上画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			UFRM_MSG_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_MSG, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_MSG );
	}
	{	// 上画面　テキストフレーム面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			UFRM_FRAME_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_FRAME, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_FRAME );
	}
	{	// 上画面　背景面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			UFRM_BACK_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_BACK, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_BACK );
	}

	{	// 下画面　メッセージ面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			DFRM_MSG_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_MSG, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_MSG );
	}
	{	// 下画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			DFRM_TEXT_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_TEXT, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_TEXT );
	}
	{	// 下画面　デザイン面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			DFRM_DESIGN_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_DESIGN, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_DESIGN );
	}
	{	// 下画面　背景面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe000, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			DFRM_BACK_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_BACK, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_BACK );
	}

	GF_BGL_ClearCharSet(UFRM_BACK,32,0,wk->heapID);
	GF_BGL_ClearCharSet(DFRM_MSG,32,0,wk->heapID);
	GF_BGL_ClearCharSet(DFRM_BACK,32,0,wk->heapID);
}

/**
 *	@brief	BGL解放
 */
static void sub_BGLRelease(SAFARIO_WORK* wk)
{
	int i;

	GF_BGL_BGControlExit( wk->bgl, DFRM_BACK);
	GF_BGL_BGControlExit( wk->bgl, DFRM_DESIGN);
	GF_BGL_BGControlExit( wk->bgl, DFRM_TEXT);
	GF_BGL_BGControlExit( wk->bgl, DFRM_MSG);

	GF_BGL_BGControlExit( wk->bgl, UFRM_BACK);
	GF_BGL_BGControlExit( wk->bgl, UFRM_FRAME);
	GF_BGL_BGControlExit( wk->bgl, UFRM_MSG);
	sys_FreeMemoryEz(wk->bgl);

	//上画面をメイン,下画面をサブに戻す
//	GX_SetDispSelect(GX_DISP_SELECT_MAIN_SUB);
}

/**
 *	@brief	2DBGリソース初期化
 */
static void sub_2DResInit(SAFARIO_WORK* wk)
{
	ARCHANDLE* handle;

	//アーカイブハンドル取得
	handle = ArchiveDataHandleOpen(ARC_SAFARIO_GRA,wk->heapID);

	//キャラクタ転送
	ArcUtil_HDL_BgCharSet( handle, NARC_safario_gra_bgu_back_ncgr, wk->bgl,
		UFRM_BACK, 0, 0, FALSE, wk->heapID );
	ArcUtil_HDL_BgCharSet( handle, NARC_safario_gra_bgd_back_ncgr, wk->bgl,
		DFRM_BACK, 0, 0, FALSE, wk->heapID );

	//パレット転送
	ArcUtil_HDL_PalSet( handle, NARC_safario_gra_bgu_nclr, PALTYPE_MAIN_BG, 0, 0, wk->heapID );
	ArcUtil_HDL_PalSet( handle, NARC_safario_gra_bgd_nclr, PALTYPE_SUB_BG, 0, 0, wk->heapID );

	TalkFontPaletteLoad( PALTYPE_MAIN_BG, TALK_FONT_PAL*0x20, wk->heapID );
	TalkFontPaletteLoad( PALTYPE_SUB_BG, TALK_FONT_PAL*0x20, wk->heapID );

	//スクリーン転送
	ArcUtil_HDL_ScrnSet( handle, NARC_safario_gra_bgu_back_nscr, wk->bgl,
		UFRM_BACK, 0, 0, FALSE, wk->heapID );
	ArcUtil_HDL_ScrnSet( handle, NARC_safario_gra_bgu_frm_nscr, wk->bgl,
		UFRM_FRAME, 0, 0, FALSE, wk->heapID );
	ArcUtil_HDL_ScrnSet( handle, NARC_safario_gra_bgd_back01_nscr, wk->bgl,
		DFRM_BACK, 0, 0, FALSE, wk->heapID );

	//スクリーンリソース取得
	wk->pSBufParts = ArcUtil_HDL_ScrnDataGet(handle,
		NARC_safario_gra_bgd_parts_nscr,FALSE, &wk->pSParts,wk->heapID);
	wk->pSBufBg01 = ArcUtil_HDL_ScrnDataGet(handle,
		NARC_safario_gra_bgd_back01_nscr,FALSE,	&wk->pSBg01,wk->heapID);
	wk->pSBufBg02 = ArcUtil_HDL_ScrnDataGet(handle,
		NARC_safario_gra_bgd_back02_nscr,FALSE,	&wk->pSBg02,wk->heapID);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );

	//会話ウインドウグラフィック転送
	TalkWinGraphicSet(wk->bgl,DFRM_MSG,
		BMP_TALK_WIN_CGX, BMP_TALK_WIN_PAL,wk->win_type, wk->heapID);
	TalkFontPaletteLoad( PALTYPE_MAIN_BG, TALK_FONT_PAL*0x20, wk->heapID );

	//スクリーン転送
	GF_BGL_LoadScreenV_Req(wk->bgl,DFRM_BACK);
	GF_BGL_LoadScreenV_Req(wk->bgl,UFRM_BACK);

	//ブレンド設定
	G2_SetBlendAlpha(ALPHA_MASK_1ST,ALPHA_MASK_2ND,ALPHA_SECOND,ALPHA_FIRST);
}

/**
 *	@brief	2DBGリソース解放
 */
static void sub_2DResRelease(SAFARIO_WORK* wk)
{
	G2_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,31,0);

	sys_FreeMemoryEz(wk->pSBufBg02);
	sys_FreeMemoryEz(wk->pSBufBg01);
	sys_FreeMemoryEz(wk->pSBufParts);
}

/**
 *	@brief	3Dシステム初期化
 */
static void sub_3DResInit(SAFARIO_WORK* wk)
{
	SafariObj_3DInit(&wk->sys3d,wk->anm_time_zone,wk->heapID);
}

/**
 *	@brief	3Dシステム解放
 */
static void sub_3DResRelease(SAFARIO_WORK* wk)
{
	SafariObj_3DRelease(&wk->sys3d);
}


/**
 *	@brief	メッセージデータ初期化
 */
static void sub_MsgDataInit(SAFARIO_WORK* wk)
{
	int i = 0;

	//ここでボタンフォントも追加しておく
	FontProc_LoadFont( FONT_TOUCH, wk->heapID );

	wk->msg.man = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,
			NARC_msg_safari_o_dat,wk->heapID);

	wk->msg.wset = WORDSET_CreateEx(SAFARIO_WORDSET_BUFNUM,SAFARIO_WORDSET_BUFLEN,wk->heapID);
	wk->msg.tmp = STRBUF_Create(SAFARIO_BUFLEN,wk->heapID);
	wk->msg.str = STRBUF_Create(SAFARIO_BUFLEN,wk->heapID);

	wk->msg.page = MSGMAN_AllocString(wk->msg.man,msg_safario_page);
	wk->msg.back = MSGMAN_AllocString(wk->msg.man,msg_safario_back);
	for(i = 0;i < MSGIDX_EXP_MAX;i++){
		wk->msg.exp[i] = MSGMAN_AllocString(wk->msg.man,msg_safario_exp01+i);
	}
}

/**
 *	@brief	メッセージデータ解放
 */
static void sub_MsgDataRelease(SAFARIO_WORK* wk)
{
	int i = 0;

	for(i = 0;i < MSGIDX_EXP_MAX;i++){
		STRBUF_Delete(wk->msg.exp[i]);
	}
	STRBUF_Delete(wk->msg.back);
	STRBUF_Delete(wk->msg.page);
	STRBUF_Delete(wk->msg.str);
	STRBUF_Delete(wk->msg.tmp);

	WORDSET_Delete(wk->msg.wset);
	MSGMAN_Delete(wk->msg.man);

	FontProc_UnloadFont( FONT_TOUCH );
}

/**
 *	@brief	BMPウィンドウ初期化
 */
static void sub_BmpWinAdd(SAFARIO_WORK* wk)
{
	int i;
	u16 cgx;
	static const BMPWIN_DAT windat[] = {
	 { BMPL_MSG_FRM,BMPL_MSG_PX,BMPL_MSG_PY,BMPL_MSG_SX,BMPL_MSG_SY,
	   BMPL_MSG_PAL,BMPL_MSG_CGX},
	 { BMPL_TMSG_FRM,BMPL_TMSG_PX,BMPL_TMSG_PY,BMPL_TMSG_SX,BMPL_TMSG_SY,
	   BMPL_TMSG_PAL,BMPL_TMSG_CGX},
	 { BMPL_BACK_FRM,BMPL_BACK_PX,BMPL_BACK_PY,BMPL_BACK_SX,BMPL_BACK_SY,
	   BMPL_BACK_PAL,BMPL_BACK_CGX},
	 { BMPL_PAGE_FRM,BMPL_PAGE_PX,BMPL_PAGE_PY,BMPL_PAGE_SX,BMPL_PAGE_SY,
	   BMPL_PAGE_PAL,BMPL_PAGE_CGX},
	 { BMPL_TARGET_FRM,BMPL_TARGET_PX,BMPL_TARGET_PY,BMPL_TARGET_SX,BMPL_TARGET_SY,
	   BMPL_TARGET_PAL,BMPL_TARGET_CGX},
	 { BMPL_EXP_FRM,BMPL_EXP_PX,BMPL_EXP_PY,BMPL_EXP_SX,BMPL_EXP_SY,
	   BMPL_EXP_PAL,BMPL_EXP_CGX},
	};

	for(i = 0;i <= WIN_EXP;i++){
		GF_BGL_BmpWinAddEx(wk->bgl,&(wk->win[i]),&windat[i]);
		GF_BGL_BmpWinDataFill(&(wk->win[i]),FBMP_COL_NULL);
	}
	//リストウィンドウ追加
	cgx = BMPL_LIST_CGX;
	for(i = 0;i < PAGE_ITEM_MAX;i++){
		GF_BGL_BmpWinAdd( wk->bgl, &wk->win[WIN_LIST01+i],BMPL_LIST_FRM,
			BMPL_LIST_PX+(BMPL_LIST_OX*(i%2)),
			BMPL_LIST_PY+(BMPL_LIST_OY*(i/2)),
			BMPL_LIST_SX, BMPL_LIST_SY, BMPL_LIST_PAL, cgx);
		cgx -= BMPL_LIST_SIZ;
	}
	//YesNoボタンシステムワーク確保
	wk->ynbtn_wk = TOUCH_SW_AllocWork(wk->heapID);
}

/**
 *	@brief	BMPウィンドウ解放
 */
static void sub_BmpWinDelete(SAFARIO_WORK* wk)
{
	int i;

	//YesNoボタンシステムワーク解放
	TOUCH_SW_FreeWork( wk->ynbtn_wk);

	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinOff(&(wk->win[i]));
		GF_BGL_BmpWinDel(&(wk->win[i]));
	}
}

/**
 *	@brief	アクターリソース初期化
 */
static void sub_ActResInit(SAFARIO_WORK* wk)
{
	initVramTransferManagerHeap(32,wk->heapID);

	//セルアクターシステム初期化
	wk->pActSys = CATS_AllocMemory(wk->heapID);

	CATS_SystemInit( wk->pActSys, &safario_coi,&safario_ccmm, SAFARIO_ACT_PALRES_MAX );

	//セルアクターシステム初期化
	wk->pActRes = CATS_ResourceCreate(wk->pActSys);
	CATS_ClactSetInit( wk->pActSys, wk->pActRes, SAFARIO_ACTMAX);

	CATS_ResourceLoadBinaryEx( wk->pActSys, wk->pActRes,
		&safario_actres_list,CATS_CGXOFS_AREA,CATS_PLTTOFS_AREA);

	//OAM初期化
	REND_OAM_UtilOamRamClear_Main(wk->heapID);
	REND_OAM_UtilOamRamClear_Sub(wk->heapID);
}

/**
 *	@brief	アクターリソース解放
 */
static void sub_ActResRelease(SAFARIO_WORK* wk)
{
	CATS_ResourceDestructor(wk->pActSys,wk->pActRes);
	CATS_FreeMemory(wk->pActSys);
	wk->pActSys = NULL;
	wk->pActRes = NULL;
	DellVramTransferManager();

	//OAM初期化
	REND_OAM_UtilOamRamClear_Main(wk->heapID);
	REND_OAM_UtilOamRamClear_Sub(wk->heapID);
}

/**
 *	@brief	アクター作成
 */
static void sub_MakeActor(SAFARIO_WORK* wk)
{
	int i;

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
	{
		{	// カーソル
			RESACT_NORMAL,	//ID
			0,0,0,	//x,y,z
			ANM_CURSOR, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 戻るボタン
			RESACT_NORMAL,	//ID
			BACK_BTN_PX,BACK_BTN_PY,0,	//x,y,z
			ANM_BACK_N, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// やじるし
			RESACT_NORMAL,	//ID
			ALLOW_L_PX,ALLOW_PY,0,	//x,y,z
			ANM_AL_LN, 3, 0, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// やじるし
			RESACT_NORMAL,	//ID
			ALLOW_R_PX,ALLOW_PY,0,	//x,y,z
			ANM_AL_RN, 3, 0, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
	};

	for(i = 0;i < SAFARIO_ACTMAX;i++){
		wk->pAct[i] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[i]);
		CLACT_SetDrawFlag(wk->pAct[i], TRUE );
	}
	CATS_ObjectBGPriSet(wk->pAct[ACT_CURSOR], DFRM_TEXT_PRI);
//	CATS_ObjectEnable(wk->pAct[ACT_CURSOR], FALSE);
}

/**
 *	@brief	アクター削除
 */
static void sub_DeleteActor(SAFARIO_WORK* wk)
{
	int i;
	for(i = 0;i < SAFARIO_ACTMAX;i++){
		CATS_ObjectDel(wk->pAct[i]);
	}
}

/**
 *	@brief	expウィンドウフレーム描画
 */
static void sub_ExpWinFrameDraw(SAFARIO_WORK* wk,u8 pat)
{
#if 0
	//クリアしてから
	GF_BGL_ScrFill(wk->bgl,MFRM_MSG,0x0000,
		BMPL_WARN_PX-1,BMPL_WARN_PY,BMPL_WARN_SX+2,BMPL_WARN_SY,GF_BGL_SCRWRT_PALIN);

	//書き込み
	GF_BGL_ScrWriteExpand(wk->bgl,MFRM_MSG,
		BMPL_EXP_PX-1,BMPL_EXP_PY-(pat*2),
		BMPL_EXP_SX+2,BMPL_EXP_SY+(pat*2),wk->pSWin->rawData,0,pat*BMPL_EXP_SY,
		wk->pSWin->screenWidth/8,wk->pSWin->screenHeight/8);
	GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_MSG);
#endif
}

/**
 *	@brief	expウィンドウメッセージ描画
 */
static void sub_ExpWinDraw(SAFARIO_WORK* wk,u8 widx,u8 midx,u8 draw_f)
{
	STRBUF** buf;
	u8 ofsy,ofsx;
#if 0
	if(widx != WIN_BACK){
		buf = wk->msg.exp;
		ofsx = 0;
		ofsy = 4;
		GF_BGL_BmpWinDataFill(&wk->win[widx],EXPMSG_COL_BACK);
	}else{
		buf = wk->msg.sel;
		ofsx = ((GF_BGL_BmpWinGet_SizeX(&wk->win[widx])*8) -
				FontProc_GetPrintStrWidth( FONT_SYSTEM,buf[midx], 0 ))/2;
		ofsy = 4;
//		GF_BGL_BmpWinFill(&wk->win[idx],EXPMSG_COL_BACK,0,4,BMPL_BACK_SX*8,16);
	}

	GF_STR_PrintColor(&wk->win[widx],FONT_SYSTEM,
		buf[midx],ofsx,ofsy,MSG_ALLPUT,EXPMSG_FCOL,NULL);
#endif
}

/**
 *	@brief	戻るボタン描画
 */
static void sub_BackButtonWrite(SAFARIO_WORK* wk,BACK_BTN_STATE state)
{
	u32 siz;
	static const GF_PRINTCOLOR col[] = {
	 GF_PRINTCOLOR_MAKE(FBMP_COL_BLUE,FBMP_COL_WHITE,0),
	 GF_PRINTCOLOR_MAKE(FBMP_COL_BLUE,FBMP_COL_WHITE,0),
	 GF_PRINTCOLOR_MAKE(FBMP_COL_BLACK,FBMP_COL_BLK_SDW,0),
	};

	CLACT_AnmChg(wk->pAct[ACT_BACK], ANM_BACK_N+state);
	CLACT_AnmReStart( wk->pAct[ACT_BACK]);

	GF_BGL_BmpWinDataFill( &wk->win[WIN_BACK], 0);

	siz = FontProc_GetPrintStrWidth( FONT_TOUCH, wk->msg.back, 0 );
	GF_STR_PrintColor( &wk->win[WIN_BACK], FONT_TOUCH, wk->msg.back,
		((BMPL_BACK_SX*8)-siz)/2,4, MSG_NO_PUT, col[state], NULL );

	GF_BGL_BmpWinOnVReq(&wk->win[WIN_BACK]);
}

/**
 *	@brief	BG表示セット
 */
static void sub_BackViewSet(SAFARIO_WORK* wk,u8 mode)
{
	NNSG2dScreenData* pbg;

	if(mode == VIEW_SELECT){
		pbg = wk->pSBg01;
	}else{
		pbg = wk->pSBg02;
	}
	GF_BGL_ScrFill( wk->bgl , DFRM_TEXT, 0x0000, 0, 5, 32, 14,GF_BGL_SCRWRT_PALIN );
	GF_BGL_ScrFill( wk->bgl , DFRM_DESIGN, 0x0000, 0, 5, 32, 14,GF_BGL_SCRWRT_PALIN );

	GF_BGL_ScrWriteExpand(wk->bgl,DFRM_BACK,
		0,0,32,24,pbg->rawData,0,0,
		pbg->screenWidth/8,pbg->screenHeight/8);

	GF_BGL_LoadScreenV_Req(wk->bgl,DFRM_TEXT);
	GF_BGL_LoadScreenV_Req(wk->bgl,DFRM_DESIGN);
	GF_BGL_LoadScreenV_Req(wk->bgl,DFRM_BACK);
}

/*
 *	@brief	ページ描画コントロール
 */
static void sub_PageViewSet(SAFARIO_WORK* wk)
{
	int i,siz,idx = 0;
	u8	place_f = 0;

	idx = wk->page*PAGE_ITEM_MAX;

	GF_BGL_ScrFill( wk->bgl , DFRM_DESIGN, 0x0000, 0, 5, 32, 14,GF_BGL_SCRWRT_PALIN );
	for(i = 0;i < PAGE_ITEM_MAX;i++){
		idx  = wk->page*PAGE_ITEM_MAX+i;
		GF_BGL_BmpWinDataFill(&wk->win[WIN_LIST01+i], 0);
		if( idx >= wk->list_max ){
			GF_BGL_BmpWinOnVReq(&wk->win[WIN_LIST01+i]);
			continue;
		}
		//モード＆サイズチェック
		if(sub_CheckNotPlaceObj(wk,idx) > 0){
			place_f = 1;
		}else{
			place_f = 0;
		}
		GF_BGL_ScrWriteExpand(wk->bgl,DFRM_DESIGN,
			PANEL_CPX+(PANEL_COX*(i%2)),PANEL_CPY+(PANEL_COY*(i/2)),
			PANEL_CSX,PANEL_CSY,wk->pSParts->rawData,PANEL_CSX*place_f,0,
			wk->pSParts->screenWidth/8,wk->pSParts->screenHeight/8);
		MSGMAN_GetString(wk->msg.man,msg_object01+wk->obj[idx].id,wk->msg.str);
		siz = (BMPL_LIST_SX*8)-FontProc_GetPrintStrWidth( FONT_TOUCH, wk->msg.str, 0);
		GF_STR_PrintColor(&wk->win[WIN_LIST01+i],FONT_TOUCH,
			wk->msg.str,siz/2,0,MSG_NO_PUT,FCOL_PANEL_W,NULL);
		GF_BGL_BmpWinOnVReq(&wk->win[WIN_LIST01+i]);
	}
	//ページナンバー表示
	GF_BGL_BmpWinDataFill(&wk->win[WIN_PAGE], 0);
	WORDSET_RegisterNumber(wk->msg.wset,0,wk->page+1,1,NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT);
	WORDSET_RegisterNumber(wk->msg.wset,1,wk->page_max,1,NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT);
	WORDSET_ExpandStr(wk->msg.wset,wk->msg.tmp,wk->msg.page);

	GF_STR_PrintColor(&wk->win[WIN_PAGE],FONT_SYSTEM,
		wk->msg.tmp,0,0,MSG_NO_PUT,FCOL_SYS_W,NULL);
	GF_BGL_BmpWinOnVReq(&wk->win[WIN_PAGE]);

	GF_BGL_LoadScreenV_Req(wk->bgl,DFRM_TEXT);
	GF_BGL_LoadScreenV_Req(wk->bgl,DFRM_DESIGN);

	//矢印アクター表示状態セット
	if(wk->page == 0){
		CLACT_AnmChg(wk->pAct[ACT_ALLOW_L],ANM_AL_LP);
	}else{
		CLACT_AnmChg(wk->pAct[ACT_ALLOW_L],ANM_AL_LN);
	}
	if(wk->page == (wk->page_max-1)){
		CLACT_AnmChg(wk->pAct[ACT_ALLOW_R],ANM_AL_RP);
	}else{
		CLACT_AnmChg(wk->pAct[ACT_ALLOW_R],ANM_AL_RN);
	}
}

/*
 *	@brief	ターゲットViewセット
 */
static void sub_TargetViewSet(SAFARIO_WORK* wk)
{
	int siz;

	GF_BGL_BmpWinDataFill(&wk->win[WIN_TARGET], 0);
	MSGMAN_GetString(wk->msg.man,msg_object01+wk->obj[wk->target].id,wk->msg.str);

	siz = (BMPL_TARGET_SX*8)-FontProc_GetPrintStrWidth( FONT_TOUCH, wk->msg.str, 0 );
	GF_STR_PrintColor(&wk->win[WIN_TARGET],FONT_TOUCH,
		wk->msg.str,siz/2,0,MSG_NO_PUT,FCOL_PANEL_W,NULL);
	GF_BGL_BmpWinOnVReq(&wk->win[WIN_TARGET]);

	GF_BGL_BmpWinOffVReq(&wk->win[WIN_PAGE]);
}


/**
 *	@brief	カーソルパターンセット
 */
static void sub_CursorVisibleSet(SAFARIO_WORK* wk,u8 idx,u8 pat,BOOL draw_f)
{
	if(!draw_f){
		CLACT_SetDrawFlag(wk->pAct[idx],FALSE);
		return;
	}
	CLACT_SetDrawFlag(wk->pAct[idx],TRUE);
	CLACT_AnmChg(wk->pAct[idx], pat);
}

/**
 *	@brief	カーソル表示位置セット
 */
static void sub_CursorPosSet(SAFARIO_WORK* wk,u8 pos)
{
	u8 pat = 0;
	s16 px,py;

	if(pos >= CPOS_BACK){
		px = BACK_BTN_PX;
		py = BACK_BTN_PY;
		pat = ANM_BCURSOR;
	}else{
		px = TP_PANEL_PX01+(TP_PANEL_OX*(pos%2));
		py = TP_PANEL_PY01+(TP_PANEL_OY*(pos/2));
		pat = ANM_CURSOR;
	}
	CATS_ObjectPosSet(wk->pAct[ACT_CURSOR], px, py);
	CLACT_AnmChg(wk->pAct[ACT_CURSOR], pat);
}

/**
 *	@brief	下画面メッセージPut
 */
static void sub_SysMsgPut(SAFARIO_WORK* wk,SYSMSG_IDX idx)
{
	BOOL expand = FALSE;
	GF_BGL_BmpWinDataFill(&wk->win[WIN_MSG], 0);

	switch(idx){
	case SYSMSG_IS_PLACE:
		MSGMAN_GetString(wk->msg.man,msg_object01+wk->target_obj,wk->msg.tmp);
		WORDSET_RegisterWord( wk->msg.wset, 0, wk->msg.tmp , PM_FEMALE, FALSE, PM_LANG );
		expand = TRUE;
		break;
	case SYSMSG_REST_NUM:
		if(wk->rest_obj == 0){
			idx = SYSMSG_NO_PLACE;
			break;
		}
		WORDSET_RegisterNumber(wk->msg.wset,0,wk->rest_obj,
			2,NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT);
		expand = TRUE;
		break;
	}
	if(expand){
		MSGMAN_GetString(wk->msg.man,msg_safario_exp01+idx,wk->msg.tmp);
		WORDSET_ExpandStr(wk->msg.wset,wk->msg.str,wk->msg.tmp);
	}else{
		MSGMAN_GetString(wk->msg.man,msg_safario_exp01+idx,wk->msg.str);
	}
	GF_STR_PrintColor(&wk->win[WIN_MSG],FONT_SYSTEM,
		wk->msg.str,0,0,MSG_NO_PUT,FCOL_SYS_W,NULL);
	GF_BGL_BmpWinOnVReq(&wk->win[WIN_MSG]);
}

/**
 *	@brief	下画面会話メッセージPut
 */
static void sub_TalkMsgPut(SAFARIO_WORK* wk,TALKMSG_IDX idx)
{
	GF_BGL_BmpWinDataFill(&wk->win[WIN_TMSG], FBMP_COL_WHITE);

	MSGMAN_GetString(wk->msg.man,msg_safario_talk01+idx,wk->msg.str);

	BmpTalkWinWrite(&wk->win[WIN_TMSG], WINDOW_TRANS_OFF, BMP_TALK_WIN_CGX, BMP_TALK_WIN_PAL );
	GF_STR_PrintColor(&wk->win[WIN_TMSG],FONT_TALK,
		wk->msg.str,0,0,MSG_NO_PUT,FCOL_W_BLACK,NULL);
	GF_BGL_BmpWinOnVReq(&wk->win[WIN_TMSG]);
}

/**
 *	@brief	上画面　オブジェ説明メッセージ
 */
static void sub_ExpMsgPut(SAFARIO_WORK* wk)
{
	u8 target = 0;

	sub_ModelChangeReq(wk);

	GF_BGL_BmpWinDataFill(&wk->win[WIN_EXP], 0);

	target = wk->page*PAGE_ITEM_MAX+wk->pos;
	if(wk->pos >= PAGE_ITEM_MAX || target >= wk->list_max){
		GF_BGL_BmpWinOnVReq(&wk->win[WIN_EXP]);
		return;
	}

	MSGMAN_GetString(wk->msg.man,msg_object_exp01+wk->obj[target].id,wk->msg.str);
	GF_STR_PrintColor(&wk->win[WIN_EXP],FONT_SYSTEM,
		wk->msg.str,0,0,MSG_NO_PUT,FCOL_EXP_W,NULL);
	GF_BGL_BmpWinOnVReq(&wk->win[WIN_EXP]);
}

/**
 *	@brief	初期画面描画
 */
static void sub_InitialDraw(SAFARIO_WORK* wk)
{
	SafariObj_ViewChange(wk,VIEW_SELECT);
	sub_CursorPosSet(wk,wk->pos);
	sub_ExpMsgPut(wk);
}

//////////////////////////////////////////////////////////////////////////////////
/**
 *	@brief	ブロックチェンジ画面　ページスクロール初期化
 */
static void eff_PageScrollInit(SAFARIO_WORK* wk)
{
}

/**
 *	@brief	ブロックチェンジ画面　ページスクロール
 */
static BOOL eff_PageScroll(SAFARIO_WORK* wk)
{
	return FALSE;
}

/*
 *	@brief	指定オブジェクトが置けるかどうかチェック
 */
static int sub_CheckNotPlaceObj(SAFARIO_WORK* wk,u8 id)
{
	//OBJの残り数チェック
	if(wk->rest_obj == 0){
		return 4;
	}
	//モードチェック
	if(wk->param->place_mode != wk->obj[id].prm.mode_f){
		if(wk->param->place_mode == SAFOBJ_PLACE_MODE_GROUND){
			return 2;
		}
		return 3;
	}
	//サイズチェック
	if(!wk->param->obj_spc[wk->obj[id].siz].valid){
		return 1;
	}
	return 0;
}

/**
 *	@brief	ページ切替タスクセット
 *
 *	@param	wk		SAFARIO_WORKへのポインタ
 *	@param	dir		ページ切替方向
 *	@param	tcb_ct	タスクの動作数を監視するカウンタへのポインタ(NULL指定可)
 *	@param	pri	タスクの動作プライオリティ
 *	@param	heapID	利用するヒープ
 *
 *	tcb_ctへのポインタは、渡しておくとタスク追加時に +1し、タスクを削除するときに -1します
 */
TCB_PTR sub_PageChangeTaskSet(SAFARIO_WORK* wk,u8 dir,int* tcb_ct,u8 pri)
{
	PAGE_CHANGE_TASK* pct;

	pct = sys_AllocMemory(wk->heapID,sizeof(PAGE_CHANGE_TASK));
	MI_CpuClear8(pct,sizeof(PAGE_CHANGE_TASK));

	pct->app_wk = wk;
	pct->tcb_ct = tcb_ct;
	pct->dir = dir;

	CLACT_AnmChg(wk->pAct[ACT_ALLOW_L+dir], ANM_AL_LA+(dir*3));
//	CLACT_AnmReStart( pct->clact_obj );
//	CLACT_SetAnmFlag( pct->clact_obj ,TRUE );
	Snd_SePlay( SE_SAFARIO_PAGE_CHG );

	if(pct->tcb_ct != NULL){
		++(*pct->tcb_ct);
	}
	return TCB_Add( tcb_PageChangeTask, pct, pri);
}

/**
 *	@brief	ページ切替タスク
 */
static void tcb_PageChangeTask( TCB_PTR tcb, void* p_work )
{
	PAGE_CHANGE_TASK* pct = (PAGE_CHANGE_TASK*)p_work;

	if(pct->ct++ < 2){
		return;
	}
	sub_PageViewSet(pct->app_wk);
	sub_ExpMsgPut(pct->app_wk);
	sub_CursorPosSet(pct->app_wk,pct->app_wk->pos);

	--(*pct->tcb_ct);
	sys_FreeMemoryEz(p_work);
	TCB_Delete( tcb );
	tcb = NULL;
}

/**
 *	@brief	モデルチェンジリクエスト発行
 */
static void sub_ModelChangeReq(SAFARIO_WORK* wk)
{
	wk->mdl_chg_req = TRUE;
	SafariObj_ModelVisibleSet(&wk->sys3d,FALSE);
}